//
//  RBEmptyDataListController.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/12.
//

#import "RBEmptyDataListController.h"

@interface RBEmptyDataListController ()

@end

@implementation RBEmptyDataListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self rb_initViewUI];
}
-(void)rb_initViewUI{
    self.isShowRefresh = YES;
    self.isShowLoadBtn = NO;
    self.isShowNoNetworkText = YES;
    self.loadingState = LoadingStateNormal;
}
- (void)setAimView:(UIScrollView *)aimView {
    _aimView = aimView;
    if (_isShowRefresh) {
        [self rb_initRefresh];
    }
}
-(void)rb_initRefresh{
    //下拉刷新
    MJRefreshNormalHeader *headRef = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullDownRefreshLoadData)];
    headRef.stateLabel.textColor = [UIColor whiteColor];
    headRef.lastUpdatedTimeLabel.hidden = YES;
    self.aimView.mj_header = headRef;
    
    //上拉加载
    MJRefreshAutoNormalFooter *normalFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(pullUpRefreshLoadData)];
    //设置mj_footer底部文字
    [normalFooter setTitle:@"" forState:(MJRefreshStateNoMoreData)];
    self.aimView.mj_footer = normalFooter;
    self.aimView.mj_footer.automaticallyChangeAlpha = NO;
}
- (void)pullDownRefreshLoadData {
    [self.aimView.mj_footer resetNoMoreData];
    [self.aimView.mj_footer endRefreshing];
}
- (void)pullUpRefreshLoadData { }
#pragma mark - DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
- (void)setLoadingState:(LoadingState)loadingState {
    _loadingState = loadingState;
//    _isShowLoadBtn = loadingState == LoadingStateNoNetwork;
    _aimView.emptyDataSetSource = self;
    _aimView.emptyDataSetDelegate = self;
    // 每次 loading 状态被修改，就刷新空白页面。
    [self.aimView reloadEmptyDataSet];
}
//占位图(大图)
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    switch (_loadingState) {
        case LoadingStateNormal:
            return nil;
            break;
        case LoadingStateUnKnownError:
            return [UIImage imageNamed:@"qsy_zwxl"];
            break;
        case LoadingStateNoData:
            return [UIImage imageNamed:self.emptyDataImageName==nil?@"qsy_zwxl":self.emptyDataImageName];
            break;
        case LoadingStateNoNetwork:
            return [UIImage imageNamed:self.noNetworkImageName==nil?@"wlyc":self.noNetworkImageName];
            break;
        case LoadingStateNoNetworkNoReload:
            return [UIImage imageNamed:self.noNetworkImageName==nil?@"wlyc":self.noNetworkImageName];
            break;
        default:
            break;
    }
}
//占位图下方的文字
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSDictionary *attributes = @{
    NSFontAttributeName:RBPingFangFont(14),
    NSForegroundColorAttributeName:RBColor(153, 153, 153)
                                 };
    switch (_loadingState) {
        case LoadingStateNormal:
            return nil;
            break;
        case LoadingStateUnKnownError:
        {
            return [[NSAttributedString alloc] initWithString:@"未知错误" attributes:attributes];
        }
            break;
        case LoadingStateNoData:
        {
            NSString *title = self.emptyDataText == nil?@"没有数据哦~":self.emptyDataText;
            
            if (self.emptyDataTextAtt == nil) {
                return [[NSAttributedString alloc] initWithString:title attributes:attributes];
            }else{
                return [[NSAttributedString alloc] initWithString:title attributes:self.emptyDataTextAtt];
            }
        }
            break;
        case LoadingStateNoNetwork:
        {
            if (self.isShowNoNetworkText) {
                
                NSString *title;
                if (self.noNetworkText == nil) {
                    title = @"网络异常，请刷新重试";
                }else{
                    title = self.noNetworkText;
                }
                return [[NSAttributedString alloc] initWithString:title attributes:attributes];
            }else{
                return nil;
            }
        }
            break;
        case LoadingStateNoNetworkNoReload:
        {
            if (self.isShowNoNetworkText) {
                NSString *title;
                if (self.noNetworkText == nil) {
                    title = @"网络异常,请刷新重试";
                }else{
                    title = self.noNetworkText;
                }
                return [[NSAttributedString alloc] initWithString:title attributes:attributes];
            }else{
                return nil;
            }
        }
            break;
        default:
            break;
    }
}
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    
    NSDictionary *attributes = @{
    NSFontAttributeName:RBPingFangFont(14),
    NSForegroundColorAttributeName:RBColor(51, 51, 51)
                                 };
    switch (_loadingState) {
        case LoadingStateNormal:
            return nil;
            break;
        case LoadingStateUnKnownError:
        {
            return [[NSAttributedString alloc] initWithString:@"" attributes:attributes];
        }
            break;
        case LoadingStateNoData:
        {
            NSString *title = self.emptyDataDetailText == nil?@"":self.emptyDataDetailText;
            
            if (self.emptyDataTextAtt == nil) {
                return [[NSAttributedString alloc] initWithString:title attributes:attributes];
            }else{
                return [[NSAttributedString alloc] initWithString:title attributes:self.emptyDataTextAtt];
            }
        }
            break;
        case LoadingStateNoNetwork:
        {
            if (self.isShowNoNetworkText) {
                
                NSString *title;
                if (self.noNetworkText == nil) {
                    title = @"";
                }else{
                    title = self.emptyDataDetailText;
                }
                return [[NSAttributedString alloc] initWithString:title attributes:attributes];
            }else{
                return nil;
            }
        }
            break;
        case LoadingStateNoNetworkNoReload:
        {
            if (self.isShowNoNetworkText) {
                NSString *title;
                if (self.noNetworkText == nil) {
                    title = @"";
                }else{
                    title = self.emptyDataDetailText;
                }
                return [[NSAttributedString alloc] initWithString:title attributes:attributes];
            }else{
                return nil;
            }
        }
            break;
        default:
            break;
    }
}
//设置文字距离图片的距离
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {
    CGFloat spaceH = RBStringIsEmpty(self.emptyDataDetailText) ? 0 : 3;
    return -spaceH;
}
///设置垂直方向的偏移量
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    return -RBNAVIGATION_BAR_HEIGHT;
}
//设置刷新按钮图片
- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{
    if (!_isShowLoadBtn) return nil;
    UIEdgeInsets capInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    //改变尺寸
    UIEdgeInsets rectInsets = UIEdgeInsetsMake(0, -RBScaleWidth(120), 0, -RBScaleWidth(120));
    UIImage *image = [UIImage imageNamed:@"qsy_bg"];
    return [[image resizableImageWithCapInsets:capInsets resizingMode:UIImageResizingModeStretch] imageWithAlignmentRectInsets:rectInsets];
}
//刷新按钮的标题
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    // 设置按钮标题
    if (!_isShowLoadBtn) return nil;
    NSString *buttonTitle = @"刷新";
    if (_loadBtnText != nil) {
        buttonTitle = _loadBtnText;
    }
    NSDictionary *attributes = @{
        NSFontAttributeName:RBPingFangFont(14),
        NSForegroundColorAttributeName:RBColor(242, 48, 48)
    };
    return [[NSAttributedString alloc] initWithString:buttonTitle attributes:attributes];
}
///处理按钮的点击事件
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    if (self.reloadButtonCallbackBlock) {
        self.reloadButtonCallbackBlock();
    } else {
        [scrollView.mj_header beginRefreshing];
    }
}

@end
